import re

import pandas as pd
import numpy as np


def merge_demo():

    # 关键词数据
    df_keyword = pd.DataFrame({
        "keyid": np.arange(5),
        "keyword": ["numpy", "pandas", "matplotlib", "sklearn", "tensorflow"]
    })
    print(df_keyword.head(10))

    # 句子数据
    df_sentence = pd.DataFrame({
        "senid": np.arange(10, 17),
        "sentence": [
            "怎样用Pandas实现数据的Merge？",
            "Python之Numpy详细教程",
            "怎样使用Pandas批量拆分与合并Excel文件？",
            "怎样使用Pandas的map和apply函数？",
            "深度学习及TensorFlow简介",
            "Tensorflow和Numpy的关系",
            "基于sklearn的一些机器学习的代码"
        ]
    })
    print(df_sentence.head(10))

    # 方法1：暴力笛卡尔积 + 过滤
    df_keyword["match"] = 1
    df_sentence["match"] = 1

    pd_new = pd.merge(df_keyword,df_sentence)
    print(pd_new.head(100))

    print("-------------------------------------------------------------------------")
    # pd_new = pd.merge(left=df_keyword,right=df_sentence,left_on="keyid",right_on="senid")
    # print(pd_new.head())
    pd_new[pd_new.apply(match_func,axis=1)]
    print(pd_new.head(100))
    print("--------------------------------------------------------------------------")
    # 方法2：小表变字典做merge最后explode
    key_word_dict = {
        row.keyword : row.keyid
        for row in df_keyword.itertuples()
    }
    for keyword,keyid in key_word_dict.items():
        print(keyword,keyid)
    # print(key_word_dict)
    # 方法2：小表变字典做merge最后explode
    def merge_demo(row):
        # 新增一列，表示能匹配的keyids
        row['keyids'] = [
            keyid for key_word,keyid in key_word_dict.items()
            # 匹配  row['sentence'] 列  是否存在关键字  存在 取出ID 做keyid
            if re.search(key_word,row['sentence'],re.IGNORECASE)
        ]
        return row

    df_merge = df_sentence.apply(merge_demo,axis=1)
    print(df_merge.head(10))

    df_merge.explode("keyids")

    print(df_merge.head())

    df_result = pd.merge(
        left = df_merge.explode("keyids"),
        right = df_keyword,
        left_on = "keyids",
        right_on = "keyid"
    )
    print(df_result.head())




# 过滤结果
def match_func(row):
    return re.search(row['keyword'],row['sentence'],re.IGNORECASE) is not None



if __name__ == "__main__":
    merge_demo()